package com.chuangxin.data.why10;

import com.chuangxin.data.core.URLContentIndependentFetcher;
import com.chuangxin.data.core.io.DataOutput;
import com.chuangxin.data.core.io.FileWriter;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

/**
 * Created by Dawnwords on 2015/2/2.
 */
public class SWGWSMFetcher extends URLContentIndependentFetcher {

    private Iterator<String> urlIt;

    public SWGWSMFetcher(DataOutput output) {
        super(output);
        try {
            Document document = Jsoup.connect("http://www.10why.net/archives/").get();
            Elements as = document.select("div.sya_postcontent").select("a");
            List<String> urlList = new LinkedList<String>();
            for (int i = 0; i < as.size(); i++) {
                urlList.add(as.eq(i).attr("href"));
            }
            urlIt = urlList.iterator();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected String nextURL() {
        return urlIt.next();
    }

    @Override
    protected boolean hasNextURL() {
        return urlIt.hasNext();
    }

    @Override
    protected TitleContent processDoc(Document doc, String url) {
        String title = doc.select("h1.entry-heading > a").eq(0).html();
        Elements wrapper = doc.select("div.entry-text");
        wrapper.select("div").remove();
        String content = wrapper.html();
        return new TitleContent(title, content);
    }

    public static void main(String[] args) {
        new SWGWSMFetcher(new FileWriter("E:" + File.separator + "10why")).execute();
    }
}
